package com.sam4cloud.common.core.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Setter;

import java.util.Collections;
import java.util.List;

/**
 * @Author: Sam
 * @Date: 2022/12/12 17:23
 * @Description: 简单分页模型 移除原有page非必要参数 {@link com.baomidou.mybatisplus.extension.plugins.pagination.Page}
 */
@ApiModel(value = "MPage对象", description = "分页参数")
public class MPage<T> implements IPage<T> {

    private static final long serialVersionUID = 1L;

    /**
     * 查询数据列表
     */
    @ApiModelProperty(value = "查询数据列表", hidden = true)
    protected List<T> records = Collections.emptyList();

    /**
     * 总数
     */
    @ApiModelProperty(value = "总数", hidden = true)
    protected long total = 0;
    /**
     * 每页显示条数，默认 10
     */
    @ApiModelProperty(value = "每页显示条数，默认 10")
    protected long size = 10;

    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页")
    protected long current = 1;
    /**
     * 是否进行 count 查询
     */
    @ApiModelProperty(value = "是否进行 count true/false", hidden = true)
    protected boolean searchCount = true;

    /**
     * 最大每页分页数限制,优先级高于分页插件内的 maxLimit
     */
    @Setter
    @JsonIgnore
    @ApiModelProperty(value = "最大每页分页数限制", hidden = true)
    protected Long maxLimit = 500L;

    public MPage() {
    }

    /**
     * 分页构造函数
     *
     * @param current 当前页
     * @param size    每页显示条数
     */
    public MPage(long current, long size) {
        this(current, size, 0);
    }

    public MPage(long current, long size, long total) {
        this(current, size, total, true);
    }

    public MPage(long current, long size, boolean searchCount) {
        this(current, size, 0, searchCount);
    }

    public MPage(long current, long size, long total, boolean searchCount) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.searchCount = searchCount;
    }

    /**
     * 是否存在上一页
     *
     * @return true / false
     */
    public boolean hasPrevious() {
        return this.current > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return this.current < this.getPages();
    }

    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public MPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public MPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public MPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public MPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    public String countId() {
        return IPage.super.countId();
    }

    @Override
    public Long maxLimit() {
        return maxLimit;
    }


    @Override
    public List<OrderItem> orders() {
        return null;
    }

    @Override
    public boolean optimizeCountSql() {
        return IPage.super.optimizeCountSql();
    }

    public static <T> MPage<T> of(long current, long size, long total, boolean searchCount) {
        return new MPage<>(current, size, total, searchCount);
    }

    @Override
    public boolean optimizeJoinOfCountSql() {
        return IPage.super.optimizeJoinOfCountSql();
    }

    public MPage<T> setSearchCount(boolean searchCount) {
        this.searchCount = searchCount;
        return this;
    }

    @JsonIgnore
    @Override
    public long getPages() {
        return IPage.super.getPages();
    }

    /* --------------- 以下为静态构造方式 --------------- */
    public static <T> MPage<T> of(long current, long size) {
        return of(current, size, 0);
    }

    public static <T> MPage<T> of(long current, long size, long total) {
        return of(current, size, total, true);
    }

    public static <T> MPage<T> of(long current, long size, boolean searchCount) {
        return of(current, size, 0, searchCount);
    }

    public static <T> MPage<T> of(MPageDTO mPageDTO) {
        return of(mPageDTO.getCurrent(), mPageDTO.getSize(), 0);
    }

    @Override
    public boolean searchCount() {
        if (total < 0) {
            return false;
        }
        return searchCount;
    }


}
